package cn.zj.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.zj.logistics.pojo.MessageObject;
import cn.zj.logistics.pojo.User;
import cn.zj.logistics.pojo.UserExample;
import cn.zj.logistics.pojo.UserExample.Criteria;
import cn.zj.logistics.service.UserService;

@Controller
@RequestMapping("/admin")
public class AdminController {

	@Autowired
	private UserService userService;
	
	@RequestMapping("/adminPage")
	public String adminPage() {
		
		return "adminPage";
	}
	
	
	@RequestMapping("/list")
	@ResponseBody
	public <T> PageInfo<User> list(@RequestParam(defaultValue = "1")int pageNum,
							   @RequestParam(defaultValue = "10")int pageSize,
							   String keyword){
		
		
		
		PageHelper.startPage(pageNum, pageSize);
		
		
		UserExample example = new UserExample();
		
		
		//前台传递关键字参数不为空的时候才参与条件查询
		if(StringUtils.isNotBlank(keyword)) {
			//使用 管理员的账号和姓名搜索 OR 或者关系
			System.out.println("keyword :"+keyword);
			Criteria criteria1 = example.createCriteria();
			criteria1.andUsernameLike("%"+keyword+"%");
			
			Criteria criteria2 = example.createCriteria();
			criteria2.andRealnameLike("%"+keyword+"%");
			
			//OR关系
			example.or(criteria2);
			
			
			//根据角色名称查询 
			Criteria criteria3 = example.createCriteria();
			criteria3.andRolenameLike("%"+keyword+"%");
			//OR关系
			example.or(criteria3);
		}
		
		
		List<User> users = userService.selectByExample(example );
		
		
		PageInfo<User> pageInfo = new PageInfo<>(users);
		
		return pageInfo;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public MessageObject delete(Long userId) {
		
		int row = userService.deleteByPrimaryKey(userId);
		
		MessageObject mo = MessageObject.createMo(0, "删除数据失败，请联系技术人员");
		
		if(row == 1) {
			mo = MessageObject.createMo(1, "删除数据成功");
		}
		return mo;
	}
	
	
	
	public static void main(String[] args) {
		/*
		 * 
		 * commons-lang.x.jar
		 * 是Apache为java语言写的一个增强包
		 * 
		 * StringUtils ：字符串增强，如果判断是否为空
		 * ArraysUtils :数组增强，数组可以向list集合一样方便的增删改查操作
		 */
		
		System.out.println(StringUtils.isNotBlank(null));
		System.out.println(StringUtils.isNotBlank(""));
		System.out.println(StringUtils.isNotBlank(" "));
		System.out.println(StringUtils.isNotBlank("AA"));
		System.out.println(StringUtils.isBlank("AA"));
		
		
	}
	
	
	
	
	
}
